﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class cpntraining_lessons_add : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //增加或者修改判断。
                if (!string.IsNullOrEmpty(CommRequest.GetString("Flag")))
                {
                    this.hid_Flag.Value = CommRequest.GetString("Flag");
                }

                if (this.hid_Flag.Value == "EDIT")
                {
                    this.InitLessonsInfo(CommRequest.GetInt("LessonID", 0));
                }
                this.txt_StartDate.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                this.txt_EndDate.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                this.InitBasePageInfo();
            }
        }

        public void InitBasePageInfo()
        {
            List<Catering_Model.LessonType> _listBookType = new Catering_Rules.LessonType().GetModelList(" Status='O' ");
            this.ddlLessonTypeID.DataSource = _listBookType;
            this.ddlLessonTypeID.DataTextField = "LessonTypeName";
            this.ddlLessonTypeID.DataValueField = "LessonTypeID";
            this.ddlLessonTypeID.DataBind();
        }

        public void InitLessonsInfo(int LessonID)
        {
            Catering_Model.StaffLessons _LessonsModel = new Catering_Model.StaffLessons();
            _LessonsModel = new Catering_Rules.StaffLessons().GetModel(LessonID);
            if (_LessonsModel == null) return;
            ViewState["EditLessonsModel"] = _LessonsModel;
            this.txt_LessonName.Text = _LessonsModel.LessonName;
            this.ddlLessonTypeID.SelectedValue = _LessonsModel.LessonTypeID.ToString();
            this.rbl_Status.SelectedValue = _LessonsModel.Status;
            this.rbl_IsTest.SelectedValue = _LessonsModel.IsTest ? "1" : "0";
            this.txt_StartDate.Text = _LessonsModel.StartDate.ToString("yyyy-MM-dd");
            this.txt_EndDate.Text = _LessonsModel.EndDate.ToString("yyyy-MM-dd");
            this.txt_LearnTime.Text = _LessonsModel.LearnTime.ToString();
            this.txt_Remarks.Text = _LessonsModel.Remarks.ToString();
        }

        /// <summary>
        /// 添加或者修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sMsg = "";
            Catering_Model.StaffLessons _ClassLessonsModel = new Catering_Model.StaffLessons();
            _ClassLessonsModel.LessonName = this.txt_LessonName.Text.Trim();
            _ClassLessonsModel.LessonTypeID = int.Parse(this.ddlLessonTypeID.SelectedValue);
            _ClassLessonsModel.Status = this.rbl_Status.SelectedValue;
            _ClassLessonsModel.IsTest = this.rbl_IsTest.SelectedValue=="1"?true:false;
            _ClassLessonsModel.StartDate = Convert.ToDateTime(this.txt_StartDate.Text.Trim());
            _ClassLessonsModel.EndDate = Convert.ToDateTime(this.txt_EndDate.Text.Trim());
            _ClassLessonsModel.LearnTime = int.Parse(this.txt_LearnTime.Text.Trim()); ;
            _ClassLessonsModel.Remarks = this.txt_Remarks.Text.Trim();

            if (!new Catering_Rules.StaffLessons().CheckClassLessonsModel(_ClassLessonsModel, out sMsg))
            {
                RegisterStartupScript("保存失败！" + sMsg);
                return;
            }
            if (this.hid_Flag.Value == "EDIT")
            {
                Catering_Model.StaffLessons _OldClassLessons = (Catering_Model.StaffLessons)ViewState["EditLessonsModel"];
                _ClassLessonsModel.LessonID = _OldClassLessons.LessonID;
                _ClassLessonsModel.AddUserID = _OldClassLessons.AddUserID;
                _ClassLessonsModel.AddTime = _OldClassLessons.AddTime;
                if (new Catering_Rules.StaffLessons().Update(_ClassLessonsModel))
                {
                    //保存成功后将文件移到SlrPubbooks中
                    RegisterStartupScript("保存成功!");
                }
                else
                {
                    RegisterStartupScript("保存失败！");
                }
            }
            else
            {
                _ClassLessonsModel.AddUserID = int.Parse(Session["UserID"].ToString());
                _ClassLessonsModel.AddTime = DateTime.Now;
                if (new Catering_Rules.StaffLessons().Add(_ClassLessonsModel) > 0)
                {
                    //保存成功后将文件移到SlrPubbooks中
                    RegisterStartupScript("保存成功!");
                }
                else
                {
                    RegisterStartupScript("保存失败！");
                }
            }
        }

        public void RegisterStartupScript(string strMessage)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('" + strMessage + "！')", true);
        }

        protected void btn_Return_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/cpntraining_lessons_list.aspx";
            Response.Redirect(url);
        }
    }
}